package com.backend.mapper;


import com.backend.pojo.domain.New;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NewMapper {
    // 1 查询所有
    @Select("select * from new")
    List<New> selectAll();

    // 2 根据newNo查询
    @Select("select * from new where newNo = #{newNo}")
    New selectById(int newNo);

    // 3 多条件模糊查询
    @Select({
            "<script>",
            "SELECT * FROM new WHERE 1=1", // 始终为真，方便后续添加条件
            "<if test='newNo != null'> and newNo = #{newNo} </if>",
            "<if test='newTitle != null and newTitle != \"\"'> AND newTitle LIKE CONCAT('%', #{newTitle}, '%') </if>",
            "<if test='newContent != null and newContent != \"\"'> AND newContent LIKE CONCAT('%', #{newContent}, '%') </if>",
            "</script>"
    })
    List<New> selectByConditionMulti(New news);

    // 4 多条件中选择一个条件查询
    @Select({
            "<script>",
            "SELECT * FROM new",
            "<where>",
            "<choose>",
            "<when test='newNo != null'> newNo = #{newNo} </when>",
            "<when test='newTitle != null and newTitle != \"\"'> newTitle LIKE CONCAT('%', #{newTitle}, '%') </when>",
            "<when test='newContent != null and newContent != \"\"'> newContent LIKE CONCAT('%', #{newContent}, '%') </when>",
            "<otherwise> 1 = 1 </otherwise>",
            "</choose>",
            "</where>",
            "</script>"
    })
    List<New> selectByConditionSingle(New news);

    // 5 增加
    @Insert("insert into new(newTitle,newContent,newNo,newTime)" +
            "values(#{newTitle},#{newContent},#{newNo},#{newTime})")
    void addNew(New news);

    // 6 根据newNo修改
    @Update({
            "<script>",
            "UPDATE new",
            "<set>",
            "<if test='newTitle != null and newTitle != \"\"'> newTitle = #{newTitle}, </if>",
            "<if test='newContent != null and newContent != \"\"'> newContent = #{newContent}, </if>",
            "</set>",
            "WHERE newNo = #{newNo}",
            "</script>"
    })
    void update(New news);

    // 7 根据newNo删除一个
    @Delete("delete from new where newNo = #{newNo}")
    void deleteNewById(int newNo);

    // 8 根据int[] newNos删除多个
    @Delete({
            "<script>",
            "DELETE FROM new",
            "WHERE newNo IN (<foreach collection='newNos' item='newNo' separator=','> #{newNo} </foreach>)",
            "</script>"
    })
    void deleteManyNewByIds(@Param("newNos") int[] newNos);
}
